<template>
  <div>
    <ul>
      <li v-for="m in messageList" :key="m.id">
        <!--路由传参，格式和params参数一致，传递变量注意要用v-bind-->
        <router-link :to="`/home/message/detail?id=${m.id}&title=${m.title}`">{{m.title}}</router-link>&nbsp;&nbsp;
        
        <button @click="pushShow(m)">push查看</button>
        <button @click="replaceShow(m)">replace查看</button>
      </li>
    </ul>
    <hr>
    <!--展示Detail组件的位置-->
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: "Message",
  data() {
    return {
      messageList: [
        {id: '001', title: '消息001'},
        {id: '002', title: '消息002'},
        {id: '003', title: '消息003'},
      ]
    }
  },
  methods: {
    pushShow(m) {
      //在VueRouter原型对象上有一对push和replace方法，可以实现编程式路由
      console.log(this.$router)

      //调用push方法跳转时，传入的配置对象和router-link对象写法一模一样
      this.$router.push({
        path: '/home/message/detail',
        query: {
          id: m.id,
          title: m.title,
        }
      })
    },
    replaceShow(m) {
      //调用replace方法跳转时，传入的配置对象和router-link对象写法一模一样
      this.$router.replace({
        path: '/home/message/detail',
        query: {
          id: m.id,
          title: m.title,
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
